import { queryNews, queryReports, queryZzds, getDetail } from '@/services/headlines';
import { queryVideos } from '@/services/video';

export default {
  namespace: 'headlines',

  state: {
    news: {},
    reports: {},
    zzds: {},
    videos: {},
    detailInfo: {},
  },

  effects: {
    *getDetailInfo({ payload }, { call, put }) {
      try {
        const response = yield call(getDetail, payload);
        // console.log('新闻详情：', response);

        if (response) {
          yield put({
            type: 'setDetailInfo',
            payload: {
              detailInfo: response,
            },
          });
        } else {
          yield put({
            type: 'setDetailInfo',
            payload: {
              detailInfo: {},
            },
          });
        }
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *queryNews({ payload }, { call, put }) {
      const response = yield call(queryNews, payload.page_size, payload.page_num, payload.keyword);
      // console.log("response",response)
      yield put({
        type: 'setPayload',
        payload: {
          news: response ? response.data : {},
        },
      });
    },
    *queryReports({ payload }, { call, put }) {
      const response = yield call(
        queryReports,
        payload.page_size,
        payload.page_num,
        payload.type,
        payload.keyword
      );
      yield put({
        type: 'setPayload',
        payload: {
          reports: response ? response.data : {},
        },
      });
    },
    *queryZzds({ payload }, { call, put }) {
      const response = yield call(queryZzds, payload.page_size, payload.page_num, payload.keyword);
      yield put({
        type: 'setPayload',
        payload: {
          zzds: response ? response.data : {},
        },
      });
    },
    *queryVideos({ payload }, { call, put }) {
      const response = yield call(queryVideos, payload.type, payload.page_size, payload.page_num);
      yield put({
        type: 'setPayload',
        payload: {
          videos: response ? response.data : {},
        },
      });
    },
  },

  reducers: {
    setDetailInfo(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
